
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus: false,
    total: null,
    number: null,
    message: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  pubPackage: function (e) {
    var that = this;
    // if (!this.data.message) {
    //   wx.showModal({
    //     content: '请输入内容',
    //     showCancel: false
    //   })
    //   return false;
    // }
    
    if (!that.data.total) {
      wx.showModal({
        content: '请输入金币',
        showCancel: false
      })
      return false;
    }
    if (isNaN(that.data.total)) {
      wx.showModal({
        content: '金币必须是数字',
        showCancel: false
      })
      return false;
    }
    
    var y = String(that.data.total).indexOf(".") + 1;//获取小数点的位置
    var count = String(that.data.total).length - y;//获取小数点后的个数
    if(y > 0 && count > 2) {
      wx.showModal({
        content: '金币最多两位小数',
        showCancel: false
      })
      return false;
    } 

    if (!that.data.number) {
      wx.showModal({
        content: '请输入包个数',
        showCancel: false
      })
      return false;
    }
    if (!this.isInteger(that.data.number)) {
      wx.showModal({
        content: '金币包个数必须是整数',
        showCancel: false
      })
      return false;
    }
    //总数*100必须大于包个数，金币包平均每个不能少于0.01
    if (parseFloat(that.data.total * 100) < parseFloat(that.data.number)) {
      wx.showModal({
        content: '每个包不能小于0.01金币',
        showCancel: false
      })
      return false;
    }

    //余数不足
    if (!app.globalData.loginInfo || app.globalData.loginInfo.amount <= 0) {
      wx.showModal({
        content: '可用不足',
        showCancel: false
      })
      return false;
    }
    
    wx.request({
      url: app.globalData.urlPath + '/api/Mangox/PubPackage',
      method: 'POST',
      data: {        
        Token: app.globalData.loginInfo.Token,
        //OpenID: app.globalData.openid,
        Source: app.globalData.appSource,
        Total: that.data.total,
        Number: that.data.number,
        Message: that.data.message,
        Sender: app.globalData.loginInfo.Data.MgxHashID,
      },
      success: function (res) {
        app.globalData.loginInfo.Token = res.data.TokenUpdate;
        console.log("塞金币返回：" + JSON.stringify(res.data));
        if(res.data.Code == 0){
          //塞完金币总数后减去自己的金币
          app.globalData.loginInfo.amount = app.globalData.loginInfo.amount - that.data.total;
          app.globalData.pkgm = res.data.Data.pkgm;
          app.globalData.depk = res.data.Data.depk;
          wx.redirectTo({
            url: '/pages/mangox/pubpackageback/pubpackageback?pkh='+ app.globalData.pkgm +'&depk='+ app.globalData.depk +''
          })
        }else{
          wx.showModal({
            content: '塞金币失败:' + res.data.Msg,
            showCancel: false
          });
        }
      }
    });
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      message: e.detail.value,
    });
  },
  bindBlurTotal: function (e) {
    this.setData({
      total: e.detail.value,
    });
  },
  bindBlurNumber: function (e) {
    this.setData({
      number: e.detail.value,
    });
  },
  isInteger(obj) {
    return obj%1 === 0
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})